A Vignette for the Transportation Safety Review Paper
A Vignette for the Transportation Safety Review Paper
In this review paper, we attempt to provide a comprehensive review on transportation research and optimization models. This vignette serves as the supplementary materials to create reproducible examples in the manuscript Bridging the Gap between Transportation Safety Research and its Incorporation inOptimization Models: a Detailed Review and Perspective. Submitted to Transportation Research Part C: Emerging Technologies.
This vignette includes examples on the following five aspects:
- Bibliographic analysis: Bibliometric Summary for Transportation Safety, Bibliographic Network Matrices - Journal Names, keyword co-occurrences network, a Conceptual Structure Map
- Extracting online transportation safety data: Crash-related data, traffic flow data, and weather data.
- Descriptive analytic tools: data visualization and clustering
- An example of clustering
- Statistical methods: logistic regression and Poisson regression
To maximize the readability of this vignette, we hided all R codes by default, but readers can look into any code by clicking the code button.
1 Bibliographic analysis
To perform a quick bibliometric analysis on Web of Science Data obtained using the following combination of words without limiting the document type, years, language.
("hazmat transportation" OR "real-time crash prediction" OR ("vehicle routing" AND safety))
The search was performed using WoS Core Collection Resulted in the download of a plain txt file containing the full record with cited references for 992 results (7/30/2018 - 11:08 am ET)
Vignette for bibliometrix packge
1.1 Bibliometric Summary for Transportation Safety
pacman::p_load(bibliometrix,tidyverse,data.table,rvest)
df.trans.safety <- readFiles("data/savedrecs.txt")
M <-convert2df(df.trans.safety)##
## Converting your isi collection into a bibliographic dataframe
##
## Articles extracted 100
## Articles extracted 200
## Articles extracted 300
## Articles extracted 400
## Articles extracted 500
## Articles extracted 600
## Articles extracted 700
## Articles extracted 800
## Articles extracted 856
## Done!
##
##
## Generating affiliation field tag AU_UN from C1: Done!
# Bibliometric Analysis (Per the Bibliometrix Guide)
results <- biblioAnalysis(M, sep = ";")
# Options modified for this analysis purposes
options(width=100)
S <- capture.output(summary(object = results, k = 10, pause = FALSE))
cat("Bibliometric Summary for Transportation Safety",
S,file = "biblio-summary.txt", sep = "\n")
plot(x = results, k = 10, pause = FALSE)1.2 Bibliographic Network Matrices - Journal Names
NetMatrix <- biblioNetwork(M, analysis = "coupling",
network = "sources",
sep = ";")
net=networkPlot(NetMatrix, normalize = "salton",
weighted=NULL, n = 25,
Title = "Coupling of Journal Names",
type = "auto", size=13,size.cex=T,
remove.multiple=TRUE,labelsize=0.55,
label.n=25,label.cex=F,
cluster="optimal",edges.min = 5)1.3 keyword co-occurrences network
NetMatrix <- biblioNetwork(
M, analysis = "co-occurrences", network = "keywords", sep = ";")
net=networkPlot(
NetMatrix, normalize="salton", weighted=NULL, n = 60,
Title = "Keyword Co-Occurrences", type = "auto",
size=15, size.cex=T, remove.multiple=TRUE,labelsize=0.75,
label.n=60,label.cex=F, cluster="optimal",edges.min = 5,
label.color = TRUE, halo = TRUE)1.4 a Conceptual Structure Map
Creating a Conceptual Structure Map from the Titles Using the MCA Method with terms mentioned at least 25 times in the title
CS <- conceptualStructure(
M,field="ID_TM", method="MCA", minDegree=20,
k.max=5, labelsize=15, documents = 856)2 Extracting online transportation safety data
This section provides sources for online open-access transportation data, including both historic and real-time crash-related, traffic flow, and weathaer data. We also provided R codes to read different formats of data and convert them to compatible comma separated value (.csv) files.
2.2 Traffic flow data
2.2.1 Historical data (yearly)
FHWA has provided Annual Average Daily Traffic (AADT) from 2011 to 2017. As an illustration, the following code chunk displays the first five observations of AADT data for Missouri 2017.
# Using Missouri as an example
fhwai = foreign::read.dbf("data/missouri2017/Missouri2017.dbf")
datatable(
head(fhwai),
caption = 'Historical traffic data in Missouri, 2017',
class = 'cell-border stripe',
extensions = c('Buttons', 'ColReorder'),
options = list(
dom = 'Bfrtip', colReorder = TRUE, scrollX = TRUE,
buttons = c('copy', 'csv', 'excel', 'pdf', 'print')
))The downloaded “shape files” can be converted to different data formats (e.g., .csv) using the following R code.
2.2.2 Real-time data (<= 5 minutes)
There are several sources for getting real-time traffic data. Some of the states in the USA are equipped with loop detectors and video cameras. Departments of Transportation (DoT) can provide this data.
Further, HERE website also provides near real-time traffic data with the limitation of 250,000 APIs per month for free. The HERE Traffic API provides traffic flow and incidents information. It also allows the users to request traffic map tiles. The HERE Traffic API provides four types of traffic data:
- Traffic Incident Data: the type and location of each traffic incident, status, start and end time, and other relevant data
- Traffic Map Tile Overlays (Traffic Tiles): pre-rendered map tile overlays with traffic information
- Traffic Flow Data: real-time traffic flow data, including speed, congestion, geometry of the road segments
- Traffic Flow Availability: traffic flow information, excluding incidents in an area
pacman::p_load(XML,jsonlite,RCurl)
source("private/HEREAPIkey.R") # you need to register you own App_code and App_id
# set up example data
latitude <- 34.9859619
longitude <- -78.5900116
range <- 1800 # 30 minutes, in seconds
resolution <- 5
response_parsed <- RJSONIO::fromJSON(
RCurl::getURL(paste0(
"https://traffic.cit.api.here.com/traffic/6.2/flow/json/8/134/86&",
"?app_id=", App_id, "&app_code=", App_code)))
response_xml <- RCurl::getURL(
paste0("https://traffic.cit.api.here.com/traffic/6.2/flow.xml",
"?bbox=41.64,-87.94;42.02,-87.52", "&app_code=",App_code, "&app_id=",
App_id, "&pattern_time=2017-06-28T22:33:57Z", "&TmcTableId=A0101"))
response <- xmlParse(response.xml)2.3 Weather data
2.3.1 Historical (daily)
2.3.2 Real-time (<= 1 hour)
In this part, we show how to get both historical and real-time weather data using DarkSky API. It can be used in both Python and R. Before using the DarkSky API to get weather data, you need to register for a API key on its official website. The first 1000 API requests you make each day are free, but each API request over the 1000 daily limit will cost you $0.0001, which means a million extra API requests will cost you 100 USD.
To get weather data from the DarkSky API, you need to provide the following information on trucks:
- latitude
- longitude
- date and time
Then you can pass these three parameters to the get_forecast_for() function in darksky package in R.
source("private/DarkSkyAPIkey.R")
Sys.setenv(DARKSKY_API_KEY = myDarkSkyAPIkey) # you need to use your own "myDarkSkyAPIkey"
dat = structure(list(
latitude = c(41.3473127, 41.8189037, 32.8258477, 40.6776808, 40.2366043),
longitude = c(-74.2850908, -73.0835104, -97.0306677, -75.1450753, -76.9367494),
time = structure(c(1453101738, 1437508088, 1436195038, 1435243088, 1454270680),
class = c("POSIXct", "POSIXt"), tzone = "UTC")),
row.names = c(NA, -5L), class = "data.frame"
)
weather_dat <- pmap(
list(dat$latitude, dat$longitude, dat$time),
get_forecast_for)2.3.2.1 DarkSky returned data
For each observation (a combination of latitude, longitude, date and time), the darksky API returns a list of 3 data.frames:
- hourly weather. 24 hourly observations for each 15 weather variables in that day.
- daily weathe. 1 observations for each 34 weather variables in that day.
- current weather. 1 observations for each 15 weather variables at the assigned time point.
The variables include: apparent (feels-like) temperature, atmospheric pressure, dew point, humidity, liquid precipitation rate, moon phase, nearest storm distance, nearest storm direction, ozone, precipitation type, snowfall, sun rise/set, temperature, text summaries, uv index, wind gust, wind speed, wind direction
hourly weather
datatable(
head(weather_dat[[1]]$hourly),
caption = 'Hourly weather provided by the DarkSky API',
class = 'cell-border stripe',
extensions = c('Buttons', 'ColReorder'),
options = list(
dom = 'Bfrtip', colReorder = TRUE, scrollX = TRUE,
buttons = c('copy', 'csv', 'excel', 'pdf', 'print')
))%>%
formatStyle( 0, target= 'row', lineHeight='80%')daily weather
datatable(
head(weather_dat[[1]]$daily),
caption = 'Daily weather provided by the DarkSky API',
class = 'cell-border stripe',
extensions = c('Buttons', 'ColReorder'),
options = list(
dom = 'Bfrtip', colReorder = TRUE, scrollX = TRUE,
buttons = c('copy', 'csv', 'excel', 'pdf', 'print')
))%>%
formatStyle( 0, target= 'row', lineHeight='80%')currently weather
datatable(
head(weather_dat[[1]]$currently),
caption = 'Current weather provided by the DarkSky API',
class = 'cell-border stripe',
extensions = c('Buttons', 'ColReorder'),
options = list(
dom = 'Bfrtip', colReorder = TRUE, scrollX = TRUE,
buttons = c('copy', 'csv', 'excel', 'pdf', 'print')
))%>%
formatStyle( 0, target= 'row', lineHeight='80%')3 Descriptive analytic tools
3.1 Data visualization
3.2 Clustering
4 An example of clustering
The following codes attempts to replicate the visual clustering approach from
Van Wijk, Jarke J., and Edward R. Van Selow. 1999. “Cluster and Calendar Based Visualization of Time Series Data.” In Information Visualization, 1999.(Info Vis’ 99) Proceedings. 1999 IEEE Symposium on, 4-9. IEEE.
A brief example of applying EDA methods on traffic data is provided here. The goal of this example is to illustrate the efficiency of the mentioned tools in the transportation context. There is no predetermined way to utilize these methods. The efficiency of each method highly depends on the nature of the problem. Hence, the challenge is to choose the right tool which fits the best.
4.0.1 Collecting Data
Hourly vehicle counts data is used in this example. It provides the number of vehicles which passed along a particular segment of a road in one hour. Data is extracted from the Georgia Department of Transportation (GDoT) (Georgia Department of Transportation, 2015) for 2015 from station 121-5505 which located in Atlanta. GDoT provides data in separate sheets for each month. After extracting and cleaning data, it was combined to one sheet with 365 rows (days) and 24 columns (hours). Data can be downloaded from GDoT.
4.0.2 Clustering
It is almost impossible to understand raw data and also discover interesting patterns in it by just looking at 8760 (370 * 24) data cells. Hence, K-means clustering method is utilized here to present data in a more understandable format. K-means clustering is a common technique to explore data and discover patterns by grouping similar data to predefined (k) number of clusters. K-means clustering aims to group data into k clusters in a way to minimize the within-cluster sum of squares (WCSS). To find the optimal number of clusters, we have used a method that was suggested by Pham et al. (2005). According to the following graph two is the best number of clusters to group this data.
trafficflow.df <- readr::read_csv("data/georgia-TFdata-station-121-5505-Yr2015.csv")
trafficflow.df$Date <- as.Date(trafficflow.df$Date, format='%d-%b')
opt = Optimal_Clusters_KMeans(
as.data.frame(trafficflow.df[,4:27]), max_clusters = 10,
plot_clusters = T, criterion = 'distortion_fK', fK_threshold = 0.85,
initializer = 'optimal_init', tol_optimal_init = 0.2,
max_iters = 10000)num_clusters <- which.min(opt) # Based on the results, we should use k=2 clusters in kmeans
km = KMeans_arma(
as.data.frame(trafficflow.df[,4:27]), clusters = num_clusters,
n_iter = 10000, seed_mode = "random_subset", verbose = F, CENTROIDS = NULL)
pr = predict_KMeans(data.frame(trafficflow.df[,4:27]), km)
trafficflow.df$cluster.num <- as.vector(pr) %>% as.factor()
table(trafficflow.df$cluster.num)##
## 1 2
## 119 246
4.0.3 Visualization
Now, k-means clustering can be applied. The output of this step is a column which its value is either one or two,indicating that each row of data (day) belongs to cluster one or two. Now data is divided into two groups. However, still we need to transfer data to a visual format to somehow validate and guide the clustering process. Since our data contains temporal information, we have used Cluster Calendar View visualization technique which is introduced by Van Wijk and Van Selow (1999). In this technique, a calendar represents the temporal information of data and by using color coding, differences between clusters are distinguished. The following graph shows a cluster calendar view for our data. It clearly has found meaningful patterns in the vehicle counts data. Weekends and weekdays have different traffic patterns. Besides, it has captured some of the holidays. For example, the 4th of July (Independence Day) which is a weekday, is colored by light blue. It means that this day has a similar traffic pattern with weekends. In addition, the clustering method has identified other holidays like Martin Luther King Day, Memorial Day, Labor Day, Thanksgiving Day and Christmas Day.
col.brewer.pal <- brewer.pal(11, "Paired")
p2 <- ggcal(trafficflow.df$Date,trafficflow.df$cluster.num) +
theme(legend.position="top") +
scale_fill_manual(values = c("1"=col.brewer.pal[1], "2"=col.brewer.pal[2]))
p2Furthermore, a line chart (following graph) is used to show the average hourly traffic data for the two clusters. Results show that each cluster has different peaks and valleys. On the weekdays, 7 AM and 4 PM have the greatest number of vehicles which can be explained by the official working hours. On the other hand, on weekends, the traffic peak is around 1 PM which maybe refers to some people going out for lunch.
summary.df <- group_by(trafficflow.df,cluster.num)
summary.df <- summarise_all(summary.df,funs(mean))
plot.df <- subset(summary.df, select = -c(2:4))
plot.df <- melt(plot.df, value.name="Traffic.Flow",
variable.name="Hour",id.vars="cluster.num")
plot.df$cluster.num <- as.factor(plot.df$cluster.num)
p1 <- plot.df %>%
ggplot(aes(x = Hour, y = Traffic.Flow, group=cluster.num, color=cluster.num)) +
geom_line(size=2) + theme_bw() +
theme(legend.position="top", axis.text.x=element_text(angle=90, hjust=1)) +
scale_color_brewer("Paired")
p1To sum up, it seems that K-means clustering method was very efficient here. We applied raw data as inputs to this method and as outputs we could discover patterns (weekdays and weekends traffic patterns) and also with the help of visualization technique we obtained a considerable information about the data.
5 Statistical models
Logistic regression and Poisson regression are two most commonly used statistical models in traffic safety studies. In this section, we provides introductory statistical theories on the two models, and
5.1 Logistic regression
The most common model for traffic safety studies is logistic regression, where the response is \(Y_i=1\) if a crash occurred in a given segment/time period, and \(Y_i=0\) if no crash occurred. In logistic regression we assume that the logit of the probability of a traffic crash is a combination of a linear predictor variables:
\[ Y_i \sim \text{Bernoulli}(p_i)\\ \text{logit}(p_i) = \log \left( \frac{p_i}{1-p_i}\right) = \textbf{X}^{\prime} \boldsymbol{\beta} \]
Here
\[ \textbf{X}^{\prime} \boldsymbol{\beta} = \beta_0 + \sum_{j=1}^{p} x_{ij} \beta_j \]
where the \(x_{ij}\) are covariates. A simple linear regression can be conducted using the following R code.
# simulate data
set.seed(123)
n = 10000
fatigue = rnorm(n, 3, 1)
precipitation = rbeta(n, 1, 5)
traffic = rgamma(n, 5, 1)
p = gtools::inv.logit(-5 + 0.5*fatigue + 0.2*traffic + 0.5*precipitation)
Y = rbernoulli(n, p)
logit_dat = data.frame(Y, fatigue, traffic, precipitation)
# logistic regression
logit_fit <- glm(
Y ~ fatigue + traffic + precipitation,
family = "binomial", data = logit_dat)
summary(logit_fit)For a detailed explanation on interpreting the results of a logistic regression, the readers can refer to the example by the UCLA.
5.2 Poisson regression
Another widely used model for traffic safety studies is Poisson regression, where the response \(Y_i\) is the number of crashes.
\[ Y_i \sim \text{Poisson}(T*\lambda)\\ \log\lambda = \textbf{X}^{\prime} \boldsymbol{\beta} \]
Here \(T\) is the length/time of a trip. It is an exposure variable that indicates the number of crashes could have occurred.
# simulate data
set.seed(123)
n = 500
fatigue = rnorm(n, 3, 1)
precipitation = rbeta(n, 1, 5)
traffic = rgamma(n, 5, 1)
trip = rnorm(n, 6, 1)
explambda = exp(-5 + 0.5*fatigue + 0.2*traffic + 0.5*precipitation)
Y = rpois(n, lambda = explambda*trip)
pois_dat = data.frame(Y, trip, fatigue, traffic, precipitation)
# Poisson regression
poisson_fit <- glm(
Y ~ fatigue + traffic + precipitation,
offset = trip, family = "poisson", data = pois_dat)
summary(poisson_fit)##
## Call:
## glm(formula = Y ~ fatigue + traffic + precipitation, family = "poisson",
## data = pois_dat, offset = trip)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.9729 -0.7845 -0.4613 0.8670 3.0791
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -9.60355 0.23534 -40.808 <2e-16 ***
## fatigue 0.45258 0.05234 8.647 <2e-16 ***
## traffic 0.21955 0.02016 10.890 <2e-16 ***
## precipitation 0.33890 0.33963 0.998 0.318
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for poisson family taken to be 1)
##
## Null deviance: 856.41 on 499 degrees of freedom
## Residual deviance: 673.09 on 496 degrees of freedom
## AIC: 1167
##
## Number of Fisher Scoring iterations: 6
For a detailed explanation on interpreting the results of a Poisson regression, the readers can refer to the example by the UCLA.
References
Department of Epidemiology and Biostatistics, Saint Louis University. Email address miao.cai@slu.edu↩
Department of Industrial and Systems Engineering, Auburn University. Email address azm0127@auburn.edu↩
Carey Business School, Johns Hopkins Universitymza0052@auburn.edu↩
Farmer School of Business, Miami University. Email address fmegahed@miamioh.edu.↩